Allow custom carbon intensity configuration #863
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a new configuration option,
custom_carbon_intensity_g_co2e_kwh
, allowing you to specify a direct carbon intensity value (in gCO2e/kWh) for your energy consumption.When this value is provided (either via the
.codecarbon.config
file or theCODECARBON_CUSTOM_CARBON_INTENSITY_G_CO2E_KWH
environment variable), it overrides all other methods of determining carbon intensity, including cloud provider data, CO2 Signal API, and default geographical energy mixes.Key changes include:
codecarbon/core/config.py
to recognize the new configuration parameter (though existing mechanisms were largely sufficient).codecarbon/emissions_tracker.py
(specificallyBaseEmissionsTracker
) to read, validate (must be a positive float), and pass the custom intensity to theEmissions
class.codecarbon/core/emissions.py
to use this custom intensity value inget_cloud_emissions
andget_private_infra_emissions
if provided, bypassing other data lookups.tests/core/test_config.py
for configuration loading.tests/test_tracker.py
for validation and initialization logic inBaseEmissionsTracker
.tests/core/test_emissions.py
for theEmissions
class calculation logic using the custom value and fallback mechanisms.This feature provides you with greater flexibility and accuracy in reporting emissions, especially for on-premise setups with specific energy mixes or when default data sources are not representative.